package com.cloud.bc.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.entity.BcPaymenorders;
import com.cloud.bc.model.entity.BcPaymenways;
import com.cloud.bc.model.enums.PayStatusEnum;
import com.cloud.bc.util.BcUtils;
import com.cloud.hpj.HpjSuccessResponse;
import com.cloud.hpj.PayUtils;
import com.cloud.model.HpjPayResponse;
import com.cloud.model.HpjQueryPayStatusResponse;
import com.cloud.model.PayRequest;
import io.netty.util.internal.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 支付网关
 *
 * @author douhaichao code generator
 * @date 2021-03-05 10:16:01
 */
@Service
@AllArgsConstructor
public class BcPayService {
    private final BcPaymenordersService bcPaymenordersService;
    private final BcPaymenwaysService bcPaymenwaysService;
    private final PayUtils payUtils;
    private final Config config;
    private final BcUtils bcUtils;

    public HpjPayResponse createPay(
            HttpServletRequest httpServletRequest,
            PayRequest request) {
        //查询当前使用的通道
        BcPaymenways bcPaymenways = new BcPaymenways();
        bcPaymenways.setIsSwitch(1);
        List<BcPaymenways> bcPaymenwaysResult = bcPaymenwaysService.selectAll(bcPaymenways);
        BcPaymenways bcPaymenwaysOne = bcPaymenwaysResult.stream().findFirst().orElse(null);
        if (bcPaymenwaysOne == null) {
            return null;
        }
        String configData = bcPaymenwaysOne.getConfig();
        PayRequest payRequestData = JSONUtil.toBean(configData, PayRequest.class);
        //查询通道结束
        request.setAppid(payRequestData.getAppid());
        request.setAppsecret(payRequestData.getAppsecret());
        String webSite = config.getWebSite();
        //取消后地址
//        String aid = request.getMemo();

        request.setCallbackUrl(getCancelUrl(httpServletRequest, request.getTradeOrderId()));
        request.setNotifyUrl(webSite + "pay/hpj/weixin/paySuccess");
        request.setReturnUrl(webSite + "");
        //创建订单时把充值类型传到备注
//        request.setMemo(request.getPaymentType() + "");
        HpjPayResponse hpjPayResponse = payUtils.createPay(request);
        return hpjPayResponse;
    }

    private String getCancelUrl(HttpServletRequest request,
                                String orderNo) {
        String url = "";
        if (orderNo.contains("SE")) {
            //如果是SE说明是套餐
            url = bcUtils.gotoViewDefaul(request, "center/setmeal");
        } else if (orderNo.contains("CA")) {
            //充值喜糖
            url = bcUtils.gotoViewDefaul(request, "center/account/deposit/candy");
        }
        return config.getWebSite() + url;
    }

    public boolean notify(HpjSuccessResponse hpjSuccessResponse) {
        String status = "fail";
        int payType = 0;
        //如果是虎皮椒
        List<BcPaymenways> bcPaymenwaysResult = bcPaymenwaysService.selectAll(new BcPaymenways().setAlias("hpj"));
        BcPaymenways bcPaymenwaysOne = bcPaymenwaysResult.stream().findFirst().orElse(null);
        if (bcPaymenwaysOne == null) {
            return false;
        }
        PayRequest payRequestData = JSONUtil.toBean(bcPaymenwaysOne.getConfig(), PayRequest.class);
        status = payUtils.notify(hpjSuccessResponse, payRequestData);
        if (!status.equals("success")) {
            return false;
        }
        //更改订单状态，及第三方单号
        String orderId = hpjSuccessResponse.getTrade_order_id();
        String openOrderId = hpjSuccessResponse.getOpen_order_id();
        bcPaymenordersService.createNotifyOrder(orderId, openOrderId);
        return true;
    }

    public PayStatusEnum check(String orderNo) {
        BcPaymenorders bcPaymenorders = bcPaymenordersService.selectOne(new BcPaymenorders().setOrderId(orderNo));
        if (bcPaymenorders == null) {
            return PayStatusEnum.ERROR;
        }
        if (bcPaymenorders.getStatus() == 1) {
            return PayStatusEnum.SUCCESS;
        }
        BcPaymenways hpj = bcPaymenwaysService.selectOne(new BcPaymenways().setAlias("hpj"));
        PayRequest payRequestData = JSONUtil.toBean(hpj.getConfig(), PayRequest.class);
        HpjQueryPayStatusResponse hpjQueryPayStatusResponse = payUtils.queryPayStatus(orderNo, payRequestData);
        if (hpjQueryPayStatusResponse.getStatus().equals("success")) {
            bcPaymenordersService.createNotifyOrder(orderNo, hpjQueryPayStatusResponse.getOpen_order_id());
            return PayStatusEnum.SUCCESS;
        } else {
            return PayStatusEnum.READY;
        }
    }
}
